/**
 * UploadQuestionImpl.java
 * Version 1.0.0
 * Created on 2018年11月3日
 *
 */
package com.yincheng.oes.business.base.importexport;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yincheng.oes.business.base.entity.Question;
import com.yincheng.oes.business.base.entity.QuestionKnowlegeExt;
import com.yincheng.oes.business.base.mapper.QuestionKnowledgeOutlineMapper;
import com.yincheng.oes.business.base.mapper.QuestionMapper;
import com.yincheng.oes.business.util.ExcelUtils;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年11月3日
 * <p>
 * 功能描述：
 * <p>
 * 邮箱地址：446948434@qq.com
 */
@Service
public class UploadQuestionImpl implements Upload {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionKnowledgeOutlineMapper questionKnowledgeOutlineMapper;

    /*
     * (non-Javadoc)
     *
     * @see com.yincheng.oes.business.base.importexport.Upload#upload(java.io.InputStream, java.lang.String)
     */
    @Override
    public Response upload(InputStream is, String fileName) {
        return null;
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     *
     * @see com.yincheng.oes.business.base.importexport.Upload#batchUpload(java.io.InputStream, java.lang.String)
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response batchQuestionUpload(InputStream is, String fileName) throws Exception {
        QuestionResponse response = new QuestionResponse();
        List<List<Object>> list;
        try {
            list = ExcelUtils.getListByExcel(is, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(QuestionResponse.SERVER_ERROR);
            response.setMessage(e.getCause().getMessage());
            response.setErrorMessage(e.getMessage());
            return response;
        }
        List<Question> questions = new ArrayList<>();
        List<QuestionKnowlegeExt> outlines = new ArrayList<>();
        List<QuestionKnowlegeExt> outlineExts = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List<Object> row = list.get(i);
            Question question = new Question();
            QuestionKnowlegeExt outlineExt = new QuestionKnowlegeExt();
            String quesCode = (String) row.get(0);
            String quesType = (String) row.get(1);
            String stem = (String) row.get(2);
            String branch = (String) row.get(3);
            String relationCode = (String) row.get(4);
            String optionA = (String) row.get(5);
            String optionB = (String) row.get(6);
            String optionC = (String) row.get(7);
            String optionD = (String) row.get(8);
            String optionE = (String) row.get(9);
            String optionF = (String) row.get(10);
            String answer = (String) row.get(11);
            String analysis = (String) row.get(12);
            String outlineCode = (String) row.get(13);
            String majorType = (String) row.get(14);
            Integer year = Integer.valueOf((String) row.get(15));
            question.setQuesCode(quesCode);
            question.setQuesType(quesType);
            question.setRelationCode(relationCode);
            question.setStem(stem);
            question.setQuestion(branch);
            question.setOptionA(optionA);
            question.setOptionB(optionB);
            question.setOptionC(optionC);
            question.setOptionD(optionD);
            question.setOptionE(optionE);
            question.setOptionF(optionF);
            question.setAnswer(answer);
            question.setAnalysis(analysis);
            question.setCreateTime(new Date());
            question.setValid("1");
            questions.add(question);

            outlineExt.setOutlineCode(outlineCode);
            outlineExt.setOutlineType(majorType);
            outlineExt.setQuesCode(quesCode);
            outlineExt.setYearNum(year);
            outlines.add(outlineExt);
        }
        QuestionResponse questionResponse = ValidateUtil.checkExercises(questions);
        if (!questionResponse.getCode().equals(QuestionResponse.SUCCESS)) {
            return questionResponse;
        }
        QuestionResponse outlineResponse = ValidateUtil.checkOutline(outlines, outlineExts);
        if (!outlineResponse.getCode().equals(QuestionResponse.SUCCESS)) {
            return outlineResponse;
        }
        try {
            int i = questionMapper.batchInsert(questions);

            // 插入大纲并更新数据
            int j = 0;
            if (CollectionUtils.isNotEmpty(outlineExts)) {
                j = questionKnowledgeOutlineMapper.batchInsertOutlines(outlineExts);
                questionKnowledgeOutlineMapper.batchUpdateQuesId();
                questionKnowledgeOutlineMapper.batchUpdateOutline();
            }
            response.setCode(QuestionResponse.SUCCESS);
            response.setMessage("成功插入" + i + "条题目数据," + j + "条大纲关联数据");
        } catch (DuplicateKeyException e) {
            response.setCode(QuestionResponse.SERVER_ERROR);
            response.setMessage(e.getCause().getMessage());
            response.setErrorMessage(e.getMessage());
            throw new DuplicateKeyException(e.getMessage(), e.getCause());
        } catch (Exception e) {
            response.setCode(QuestionResponse.SERVER_ERROR);
            response.setMessage(e.getCause().getMessage());
            response.setErrorMessage(e.getMessage());
            throw new Exception(e);
        }

        return response;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.yincheng.oes.business.base.importexport.Upload#batchUnQuestionUpload(java.io.InputStream,
     * java.lang.String)
     */
    @Override
    public Response batchUnQuestionUpload(InputStream is, String fileName) {
        // TODO Auto-generated method stub
        return null;
    }

}
